/*
 * JPanelWizardEspejoStandardSelectElements.java
 *
 * Created on 29-ago-2011, 16:23:22
 */
package com.ufps.cuatro.vistas.asistente.espejo.complejo.wizard;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;

import org.jdesktop.application.Action;

import com.ufps.cuatro.CuatroApp;
import com.ufps.cuatro.controlador.render.UtilRender;
import com.ufps.cuatro.modelo.analizador.ObjetoReflexion;
import com.ufps.cuatro.util.Bandera;
import com.ufps.cuatro.util.Utileria;
import com.ufps.cuatro.util.wizard.UtilWizard;
import com.ufps.cuatro.vistas.representacion.JDialogSeleccionIntegradores;
import com.ufps.cuatro.vistas.representacion.JDialogSeleccionPropiedadesClase;

/**
 *
 * @author Carlos A. Torres C. 
 */
public class JPanelWizardEspejoStandardSelectElements extends javax.swing.JPanel implements Observer {
    private static final long serialVersionUID = 3804509231290149123L;
    
    private ArrayList<ObjetoReflexion> objElementos;

    /** Creates new form JPanelWizardEspejoStandardSelectElements */
    public JPanelWizardEspejoStandardSelectElements() {
        initComponents();
        objElementos = new ArrayList<ObjetoReflexion>();         
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListElementos = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabelInfoElemento = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(JPanelWizardEspejoStandardSelectElements.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListElementos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListElementos.setName("jListElementos"); // NOI18N
        jListElementos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListElementosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListElementos);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabelInfoElemento.setBackground(resourceMap.getColor("jLabelInfoElemento.background")); // NOI18N
        jLabelInfoElemento.setText(resourceMap.getString("jLabelInfoElemento.text")); // NOI18N
        jLabelInfoElemento.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelInfoElemento.setName("jLabelInfoElemento"); // NOI18N
        jLabelInfoElemento.setOpaque(true);

        jLabel4.setBackground(resourceMap.getColor("jLabel4.background")); // NOI18N
        jLabel4.setIcon(resourceMap.getIcon("jLabel4.icon")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel4.setName("jLabel4"); // NOI18N
        jLabel4.setOpaque(true);

        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);
        jToolBar1.setBorderPainted(false);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(JPanelWizardEspejoStandardSelectElements.class, this);
        jButton1.setAction(actionMap.get("addMetodo")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        jButton2.setAction(actionMap.get("delMetodo")); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelInfoElemento, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(10, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(10, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelInfoElemento, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void jListElementosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListElementosValueChanged
        if(!this.jListElementos.isSelectionEmpty()){
            ObjetoReflexion obj = (ObjetoReflexion) this.jListElementos.getSelectedValue();
            Method objMethod = (Method) obj.getObj();
            String strDef = "<html><b>" + objMethod.toGenericString() + "</b></html>";    
            this.jLabelInfoElemento.setText(strDef);
        }
}//GEN-LAST:event_jListElementosValueChanged

    @Action
    public void addElemento() {        
        JDialogSeleccionPropiedadesClase objDialog = new JDialogSeleccionPropiedadesClase(this);            
        objDialog.setLocationRelativeTo(UtilWizard.objWizard.getDialog());        
        CuatroApp.getApplication().show(objDialog);
    }

    @Action
    public void delElemento() {
        if(this.objElementos.isEmpty()){
            Utileria.mostrarMensaje(this, "Debe Agregar Elementos del Modelo Analizado.", "", JOptionPane.WARNING_MESSAGE);
        }
        if(this.jListElementos.isSelectionEmpty()){
            Utileria.mostrarMensaje(this, "Debe Seleccionar un Elemento.", "", JOptionPane.WARNING_MESSAGE);
        }
        if(!Utileria.confirmarOrden(this, "¿Desea Eliminar el Elemento?", ""))return;
        int i = this.jListElementos.getSelectedIndex();
        this.objElementos.remove(i);
        UtilRender.helperRenderJList(jListElementos, objElementos, 0);
        jLabelInfoElemento.setText("");
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelInfoElemento;
    private javax.swing.JList jListElementos;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        if(o instanceof Bandera){            
            Bandera objBandera = (Bandera) o;
            if(objBandera.getEstado() == Bandera.SELECCION_PROPIEDADES_CLASE_REPRESENTACION){
                objElementos.addAll((ArrayList<ObjetoReflexion>) objBandera.getObj());
		UtilRender.helperRenderJList(jListElementos, objElementos, 0);
                jLabelInfoElemento.setText("");
            }                                            
        }        
    }

    public ArrayList<ObjetoReflexion> getObjElementos() {
        return objElementos;
    }

    @Action
    public void addCDG() {
    }

    @Action
    public void addCDIM() {
        JDialogSeleccionIntegradores objDialogSeleccionIntegradores = new JDialogSeleccionIntegradores(this);
        objDialogSeleccionIntegradores.setLocationRelativeTo(UtilWizard.objWizard.getDialog());                
        CuatroApp.getApplication().show(objDialogSeleccionIntegradores);    
    }

    @Action
    public void addMetodo() {
        JDialogSeleccionPropiedadesClase objDialog = new JDialogSeleccionPropiedadesClase(this);            
        objDialog.setLocationRelativeTo(UtilWizard.objWizard.getDialog());        
        CuatroApp.getApplication().show(objDialog);
    }

    @Action
    public void delMetodo() {
        if(this.objElementos.isEmpty()){
            Utileria.mostrarMensaje(this, "Debe Agregar Elementos del Modelo Analizado.", "", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(this.jListElementos.isSelectionEmpty()){
            Utileria.mostrarMensaje(this, "Debe Seleccionar un Elemento.", "", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(!Utileria.confirmarOrden(this, "¿Desea Eliminar el Elemento?", ""))return;
        int i = this.jListElementos.getSelectedIndex();
        this.objElementos.remove(i);
        UtilRender.helperRenderJList(jListElementos, objElementos, 0);
        jLabelInfoElemento.setText("");
    }
}
